<?php

namespace app\common\model\system\merchant;


use app\common\model\BaseModel;
use app\common\model\store\StoreCategory;

class MerchantCategoryIntention extends BaseModel
{

    /**
     * @return string
     * @author: fupeng
     * @day: 2023/10/16
     */
    public static function tablePk(): string
    {
        return 'mer_category_intention_id';
    }

    /**
     * @return string
     * @author: fupeng
     * @day: 2023/10/16
     */
    public static function tableName(): string
    {
        return 'merchant_category_intention';
    }


    public function merchant()
    {
        return $this->hasOne(Merchant::class, 'mer_id', 'mer_id');
    }

    public function storeCategoryTwo()
    {
        return $this->hasOne(StoreCategory::class, 'store_category_id', 'lvl_two');
    }

    public function storeCategoryOne()
    {
        return $this->hasOne(StoreCategory::class, 'store_category_id', 'lvl_one');
    }



    /**
     * @return false|int|null
     * @author: fupeng
     * @day: 2023/10/18
     * 获取最高保证金
     */
    public function getHighestMargin($mer_id)
    {
        $data = $this->where(['mer_id'=>$mer_id,'status'=>1,'is_del'=>0])
            ->with([
                    'storeCategoryTwo'=> function ($query) {return $query->field('store_category_id,margin');},
                ])
            ->select()->toArray();
        $max = 0 ;
        foreach ($data as $v){
            if($v['storeCategoryTwo']['margin'] > $max){
                $max = $v['storeCategoryTwo']['margin'];
            }
        }
        return  $max;
    }


}
